package com.shenzhen.horselive.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.shenzhen.horselive.base.AppBarActivity;
import com.shenzhen.horselive.beans.ApplyCardBean;
import com.shenzhen.horselive.databinding.ActivityWebBinding;
import com.shenzhen.horselive.utils.JsBridger;

public class CardDetailsWebActivity extends AppBarActivity {

    ActivityWebBinding  binding ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityWebBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setStyleDark();
        setTitle("套餐详情");
        initViews();
        initData();
    }

    private void initData() {
        String  url =  getIntent().getStringExtra("url");
        if (TextUtils.isEmpty(url))
            return;
        binding.webview.loadUrl(url);
    }

    private void initViews() {

        //初始化
        WebSettings  webSettings = binding.webview.getSettings();

        webSettings.setDomStorageEnabled(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAppCacheEnabled(true);
        webSettings .setSupportZoom(false);
////调整图片至适合webview的大小
//        webSettings .setUseWideViewPort(true);
//// 缩放至屏幕的大小
//        webSettings .setLoadWithOverviewMode(true);

        ApplyCardBean.DataBean  dataBean  = (ApplyCardBean.DataBean) getIntent().getSerializableExtra("data");
        binding.webview.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return super.shouldOverrideUrlLoading(view, request);
            }
        });


        binding.webview.addJavascriptInterface(new JsBridger(getContext(),dataBean),"android");
    }


    public static void start(Context context, String url, String  title,ApplyCardBean.DataBean  dataBean) {
        Intent starter = new Intent(context, CardDetailsWebActivity.class);
        starter.putExtra("url",url);
        starter.putExtra("title",title);
        starter.putExtra("data",dataBean);
        context.startActivity(starter);
    }





}